package com.lidroid.xutils_sample;

import com.lidroid.xutils_sample.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.bundle.IBundleManager;

public class MainAbility extends Ability {
    private static final int  MY_PERMISSIONS=1;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());
        if (verifySelfPermission("ohos.permission.READ_MEDIA") != IBundleManager.PERMISSION_GRANTED) {
            // 应用未被授予权限
            if (canRequestPermission("ohos.permission.CAMERA")) {
                // 是否可以申请弹框授权(首次申请或者用户未选择禁止且不再提示)
                requestPermissionsFromUser(
                        new String[] {   "ohos.permission.WRITE_USER_STORAGE","ohos.permission.READ_USER_STORAGE","ohos.permission.READ_EXTERNAL_STORAGE",
                                "ohos.permission.WRITE_EXTERNAL_STORAGE", "ohos.permission.MICROPHONE", "ohos.permission.READ_MEDIA",

                        } , MY_PERMISSIONS);
            } else {
                // 显示应用需要权限的理由，提示用户进入设置授权
                return;
            }
        } else {
            // 权限已被授予
            return;
        }
    }
        @Override
    public void onRequestPermissionsFromUserResult (int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS: {
                // 匹配requestPermissions的requestCode
                if (grantResults.length > 0
                        && grantResults[0] == IBundleManager.PERMISSION_GRANTED) {
                    // 权限被授予
                    // 注意：因时间差导致接口权限检查时有无权限，所以对那些因无权限而抛异常的接口进行异常捕获处理
                    return;
                } else {
                    // 权限被拒绝
                    //System.exit(-1);
                    return;
                }
//                return;
            }
        }
    }
}
